package oocl.ita.eordering.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;

import oocl.ita.eordering.dao.OrderDao;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.vo.Vfood;
import oocl.ita.eordering.vo.Vorder;

@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderDao orderDao;

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Vorder> showOrderByClientId(int cId) {
		// TODO Auto-generated method stub
		List<Order> orders = orderDao.showOrderByClientId(cId);
		if (orders == null)
			return null;
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);

			try {
				List<Vfood> foodlist = mapper.readValue(order.getFoodInfo(), List.class);
				vorder.setFoodlist(foodlist);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorders.add(vorder);
		}
		return vorders;
	}

	@Override
	@Transactional
	public int addOrder(Order order) {
		// TODO Auto-generated method stub
		return orderDao.addOrder(order);
	}

	@Override
	@Transactional
	public int updateOrder(Order order) {
		// TODO Auto-generated method stub
		return orderDao.updateOrder(order);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public Vorder showOrderByOrderId(int oId) {
		// TODO Auto-generated method stub
		List<Order> orders = null;
		orders = orderDao.showOrderByOrderId(oId);
		if (orders == null){
			System.out.println("============");
			return null;
		}
			
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			try {
				List<Vfood> foodlist = mapper.readValue(order.getFoodInfo(), List.class);
				vorder.setFoodlist(foodlist);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorders.add(vorder);
		}
		return vorders.get(0);
	}

	@Override
	@Transactional
	public int complaintOrder(String complaint, int oId) {
		// TODO Auto-generated method stub
		return orderDao.complaintOrder(complaint, oId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vorder> findComplaintOrders(Integer cId) {
		// TODO Auto-generated method stub
		List<Order> orders = orderDao.findComplaintOrders(cId);
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			List<Vfood> foodlist = new ArrayList<Vfood>();
			try {
				foodlist = mapper.readValue(order.getFoodInfo(), List.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorder.setFoodlist(foodlist);
			vorders.add(vorder);

		}
		return vorders;
	}

	@Override
	public List<Order> showOrderByMerchantId(int mId) {
		return orderDao.showOrderByMerchantId(mId);
	}
}
